#pragma once

namespace AVUI
{

class InheritanceContextHelper
{
public:
#pragma region Internal Methods

    static void AddInheritanceContext(DependencyObject* pNewInheritanceContext, DependencyObject* pValue, bool* pHasMultipleInheritanceContexts, TWeakPtr<DependencyObject>* ppInheritanceContext)
    {
        if ((pNewInheritanceContext != (*ppInheritanceContext)) && !(*pHasMultipleInheritanceContexts))
        {
            if ((*ppInheritanceContext) == NULL || pNewInheritanceContext == NULL)
            {
                *ppInheritanceContext = pNewInheritanceContext;
            }
            else
            {
                *pHasMultipleInheritanceContexts = true;
                *ppInheritanceContext = NULL;
            }
            pValue->OnInheritanceContextChanged(EventArgs::get_Empty());
        }
    }

    static void ProvideContextForObject(DependencyObject* pContext, DependencyObject* pNewValue)
    {
        if (pContext != NULL)
        {
            pContext->ProvideSelfAsInheritanceContext(pNewValue, NULL);
        }
    }

    static void RemoveContextFromObject(DependencyObject* pContext, DependencyObject* pOldValue)
    {
        if (pContext != NULL && pOldValue->get_InheritanceContext() == pContext)
        {
            pContext->RemoveSelfAsInheritanceContext(pOldValue, NULL);
        }
    }

    static void RemoveInheritanceContext(DependencyObject* pOldInheritanceContext, DependencyObject* pValue, bool* pHasMultipleInheritanceContexts, TWeakPtr<DependencyObject>* ppInheritanceContext)
    {
        if (pOldInheritanceContext == (*ppInheritanceContext) && !(*pHasMultipleInheritanceContexts))
        {
            *ppInheritanceContext = NULL;
            pValue->OnInheritanceContextChanged(EventArgs::get_Empty());
        }
    }

#pragma endregion Internal Methods
};


}
